package com.local.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.threads.SetRuleCopyRunble;
import com.cz073586.common.billRules.entitys.vo.ParkBillingRuleForOneDayVo;
import com.cz073586.common.billRules.manager.ParkRulesEngineManager;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.local.dao.mapers.LocalCarTypeMapper;
import com.local.dao.mapers.LocalChargeBytimeCopyMapper;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.CarParkIn;
import com.local.model.LocalChargeBytime;
import com.local.model.LocalChargeBytimeCopy;
import com.local.model.LocalChargeBytimeCopyExample;
import com.local.service.face.LocalChargeByTimeCopyService;
import com.local.service.face.LocalChargeByTimeService;
import com.local.service.face.LogService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.BeanUtil;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;

@Service
public class LocalChargeByTimeCopyServiceImpl
		extends
		BaseServiceImpl<LocalChargeBytimeCopyExample, LocalChargeBytimeCopy, String>
		implements LocalChargeByTimeCopyService
{
	@Autowired
	private LocalChargeByTimeService localChargeByTimeService;
	
	@Autowired
	private LocalChargeBytimeCopyMapper localChargeBytimeCopyMapper;
	
	@Autowired
	private LocalCarTypeMapper localCarTypeMapper;
	
	@Autowired
	private LogService logService;
	
	@Override
	public BaseMapper<LocalChargeBytimeCopyExample, LocalChargeBytimeCopy, String> getMapper()
	{
		// TODO Auto-generated method stub
		return localChargeBytimeCopyMapper;
	}
	
	@Override
	public String saveOrUpdateParkwellMember(
			LocalChargeBytimeCopy localChargeBytimeCopy, CarParkIn carParkIn)
	{
		try
		{
			// 通过carParkIn,生成本地副本数据表
			localChargeBytimeCopy.setChargeBytimeUuid(UUIDGenerateUtil
					.generate());
			localChargeBytimeCopy.setCarInUuid(carParkIn.getUuid());
			localChargeBytimeCopyMapper.insert(localChargeBytimeCopy);
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
		return localChargeBytimeCopy.getChargeBytimeUuid();
	}
	
	/*
	 * @Override public double queryCarinBillMoney(String carInUuid,Date
	 * carInTime, Date carOutTime) { // TODO Auto-generated method stub try {
	 * long startTime = System.currentTimeMillis(); List<LocalChargeBytimeCopy>
	 * list = this.queryLsCarInUuid(carInUuid);
	 * log.debug("查询计费副本耗时queryLsCarInUuid:"
	 * +(System.currentTimeMillis()-startTime)+"毫秒"); startTime =
	 * System.currentTimeMillis(); ParkBillingRuleForOneDayVo billVo =
	 * InitRule.parserParkBillRule(list);
	 * log.debug("计算费用耗时parserParkBillRule:"+(
	 * System.currentTimeMillis()-startTime)+"毫秒");
	 * billVo.setOriginalCarInTime(carInTime);
	 * billVo.setOriginalCarOutTime(carOutTime);
	 * ParkRulesEngineManager.instance().startBill(billVo); return
	 * billVo.getAllBill(); } catch (Exception e) { // TODO: handle exception }
	 * return 0; }
	 * 
	 * @Override public ParkBillingRuleForOneDayVo queryParkBillingRule(String
	 * carInUuid) { // TODO Auto-generated method stub
	 * ParkBillingRuleForOneDayVo billVo =null; try {
	 * List<LocalChargeBytimeCopy> list = this.queryLsCarInUuid(carInUuid);
	 * billVo = InitRule.parserParkBillRule(list); return billVo; } catch
	 * (Exception e) { // TODO: handle exception return null; } }
	 */
	
	@Override
	public List<LocalChargeBytimeCopy> saveRuleCopy()
	{
		// TODO Auto-generated method stub
		List<LocalChargeBytimeCopy> li = new ArrayList<LocalChargeBytimeCopy>();
		List<LocalChargeBytime> list = localChargeByTimeService.queryRuleList();
		if (!Tools.isEmpty(list))
		{
			for (LocalChargeBytime lb : list)
			{
				LocalChargeBytimeCopy lc = new LocalChargeBytimeCopy();
				lc.setCarType(!Tools.isEmpty(lb.getCarType()) ? lb.getCarType()
						: null);
				lc.setFreeTime(!Tools.isEmpty(lb.getFreeTime()) ? lb
						.getFreeTime() : null);
				lc.setFreeTimeCharge(!Tools.isEmpty(lb.getFreeTimeCharge()) ? lb
						.getFreeTimeCharge() : null);
				lc.setBaseTime(!Tools.isEmpty(lb.getBaseTime()) ? lb
						.getBaseTime() : null);
				lc.setBaseTimeCharge(!Tools.isEmpty(lb.getBaseTimeCharge()) ? lb
						.getBaseTimeCharge() : null);
				lc.setAccountTime(!Tools.isEmpty(lb.getAccountTime()) ? lb
						.getAccountTime() : null);
				lc.setAccountTimeCharge(!Tools.isEmpty(lb
						.getAccountTimeCharge()) ? lb.getAccountTimeCharge()
						: null);
				lc.setSupassTime(!Tools.isEmpty(lb.getSupassTime()) ? lb
						.getSupassTime() : null);
				lc.setMaxCharge(!Tools.isEmpty(lb.getMaxCharge()) ? lb
						.getMaxCharge() : null);
				lc.setVersion(!Tools.isEmpty(lb.getVersion()) ? lb.getVersion()
						: null);
				lc.setCartypeName(!Tools.isEmpty(lb.getCartypeName()) ? lb
						.getCartypeName() : null);
				lc.setType(!Tools.isEmpty(lb.getType()) ? lb.getType() : null);
				lc.setSection(!Tools.isEmpty(lb.getSection()) ? lb.getSection()
						: null);
				lc.setSectionStartTime(!Tools.isEmpty(lb.getSectionStartTime()) ? lb
						.getSectionStartTime() : null);
				lc.setSectionEndTime(!Tools.isEmpty(lb.getSectionEndTime()) ? lb
						.getSectionEndTime() : null);
				lc.setAttr(!Tools.isEmpty(lb.getAttr()) ? lb.getAttr() : null);
				lc.setAttr2(!Tools.isEmpty(lb.getAttr2()) ? lb.getAttr2()
						: null);
				lc.setAttr3(!Tools.isEmpty(lb.getAttr3()) ? lb.getAttr3()
						: null);
				lc.setAttr4(!Tools.isEmpty(lb.getAttr4()) ? lb.getAttr4()
						: null);
				li.add(lc);
			}
		}
		return li;
	}
	
	@Override
	public double queryRuleForOneDay(ParkBillingRuleForOneDayVo pRuleBill,
			Date carInTime, Date carOutTime)
	{
		// TODO Auto-generated method stub
		try
		{
			
			ParkBillingRuleForOneDayVo billVo = new ParkBillingRuleForOneDayVo();
			BeanUtil.copyProperties(billVo, pRuleBill);
			billVo.setOriginalCarInTime(carInTime);
			billVo.setOriginalCarOutTime(carOutTime);
			ParkRulesEngineManager.instance().startBill(billVo);
			return billVo.getAllBill();
		} catch (Exception e)
		{
			// TODO: handle exception
			return 0;
		}
	}
	
	@Override
	public List<LocalChargeBytimeCopy> addRuleCopy(CarParkIn carParkIn)
	{
		// TODO Auto-generated method stub
		List<LocalChargeBytimeCopy> li = this.saveRuleCopy();
		String exchanuui = CloudConfigManager.instance().getCfgValueByKey(
				"exchange_id");
		for (LocalChargeBytimeCopy lb : li)
		{
			lb.setParkOrgCode(!Tools.isEmpty(carParkIn.getExchangeUuid()) ? carParkIn
					.getExchangeUuid() : exchanuui);
			this.saveOrUpdateParkwellMember(lb, carParkIn);
		}
		return li;
	}
	
	@Override
	public void setRuleUploadCopy(List<LocalChargeBytimeCopy> li)
	{
		// TODO Auto-generated method stub
		for (LocalChargeBytimeCopy localChargeBytimeCopy : li)
		{
			// 计费副本表，放入云平台副本表
			if (CloudConfigManager.instance().isOpen())
			{
				SetRuleCopyRunble upload = new SetRuleCopyRunble(
						localChargeBytimeCopy, logService);
				ThreadPoolUtil.instance().execute(upload);
			}
		}
	}
}
